/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HumanCentredSystemDesign;

import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author Martin
 */
public class Periodical extends Item{
    
    private int volume;
    private int number;
    
    public Periodical(){};
    public Periodical(String _itemIdentifier, int _number, Date _publicationDate, String _publisher, String _title, int _volume){
        this.itemIdentifier = _itemIdentifier;
        this.number = _number;
        this.publicationDate = _publicationDate;
        this.publisher = _publisher;
        this.title = _title;
        this.type = "Periodical";
        this.volume = _volume;
    }
    
    
    @Override
    public String toString(){
        return " ISSN: " +this.itemIdentifier + " \n Volume: " + this.volume +" \n Number: " + this.number + " \n Title: " + this.title  + " \n Publisher: " + this.publisher;
    }
    public int getVolume(){
        return this.volume;
    }
    public void setVolume(int _volume){
        this.volume = _volume;
    }
    public int getNumber(){
        return this.number;
    }
    public void setNumber(int _number){
        this.number = _number;
    }
    
    @Override
    public ArrayList<Copy> getItemCopies(){
        ArrayList<Copy> listOfCopies = new ArrayList<Copy>();
        Connect myConn = new Connect();
        myConn.establishConnection();
        
        listOfCopies = myConn.getItemCopiesFromDatabase("%",this.itemIdentifier );
        return listOfCopies;
    }
    
    
        @Override
    public boolean isReserved(){
        ArrayList<Reservation> listOfReservations = new ArrayList<Reservation>();
        Connect myConn = new Connect();
        myConn.establishConnection();
        try{
            listOfReservations = myConn.getReservationsFromDatabase(this.itemIdentifier, "");
            myConn.closeConnection();
            return ! listOfReservations.isEmpty();
            
        }
        catch(Exception err)
        {
            System.out.println("Cannot get Reserved Status from database: " + err.toString());
            myConn.closeConnection();
            return true;
        }
    }
       
    @Override
        public Borrower getFirstReserver(){
            Date myDate = new Date();
            String myBorrowerID = null;
            Borrower myBorrower = new Borrower();
            ArrayList<Reservation> listOfReservations = new ArrayList<Reservation>();
            Connect myConn = new Connect();
            listOfReservations = myConn.getReservationsFromDatabase(this.itemIdentifier, "");
            myConn.closeConnection();

            for ( Reservation r : listOfReservations)
            {
                if ( r.getReservationDate().before(myDate)){
                    myDate = r.getReservationDate();
                    myBorrowerID = r.getlibraryMemberID();
                }
            }

            LibraryCirculations lc = new LibraryCirculations();
            myBorrower = lc.getBorrower(myBorrowerID, "", "", "").get(0);
            return myBorrower;
        }
        
    @Override
        public String getAuthor(){
            return "";
        }
}
